var map;
var polys = [];
//地图初始化
function initMap(){
	map = new BMap.Map("main",{enableMapClick:true});
	var startPoint = {x:109.062603,y: 32.409127};
	var point = new BMap.Point(startPoint.x, startPoint.y);
	var mapStyle = {
		features: ["road", "building", "water", "land"],
		//隐藏地图上的poi
		style: "midnight" //设置地图风格为高端黑
	}
	map.centerAndZoom(point, 6);
	map.enableScrollWheelZoom(true);
	map.enableDragging(true);
	map.setDefaultCursor("crosshair");
	map.setMapStyle(mapStyle);
}

//定位
function locateMap(){
	var name = $("#cityname").val()
	if(name.length == 0){
		alert("位置名称不能为空")
	}else{
		map.centerAndZoom(name,10);	
	}
}

//转化为边界
function transformData(){
    var files = $("#file")[0].files;
    if( typeof(FileReader) !== 'undefined' ){    //H5
    	try{
    		var reader = new FileReader();
	        reader.readAsText( files[0] );            //以文本格式读取
	        reader.onload = function(evt){
	        	//读到的数据
	            var data = evt.target.result;
	            //行数据集合
	            var datas = data.split("\n");	
	            //结果字符串
	            var resultStr = "";
	            //区块ID计数(初始值为第二行数据的第二列值)
	            var zid_counter = datas[1].split(",")[1];
	            polys.push(datas[1].split(",")[1]);
	            //标记, 判断一个区块的边界点是否遍历完毕
	            var isOverFlag = false;
	            //开始遍历
	            for(var i=1;i<datas.length;i++){
	            	var settings = datas[i].split(",");
	            	//序号
	            	var order = settings[0];
	            	//当前区块id
	            	var zid = settings[1];
	            	
	            	//经度
	            	var blon = settings[4];
	            	//纬度
	            	var blat = settings[5];
	            	//起始行
	            	if(i == 1){
	            		resultStr += "var poly"+zid+"=new BMap.Polygon([\n";
	            	}
					if(zid_counter == zid){//如果是同一个zid的点
	            		resultStr += "new BMap.Point("+blon+","+blat+"),";
	            	}else{//如果zid和zid计数器当前值不一致, 说明已经跳出了这个zid, 进入了另一个zid的边界点中了
	            		if(zid != undefined){
	            			polys.push(zid);
	            			zid_counter = zid;//zid计数器变化
		            		resultStr += "]);\n";//同时也要给上一个区块polygon结尾
		            		resultStr += "var poly"+zid+"=new BMap.Polygon([\n";
	            		}
	            		
	            	}
	            	if(i == datas.length-1){
	            		resultStr += "]);\n";
	            	}
	            	
	            }
	            $("#pointsData").val(resultStr);
	            eval(resultStr);
	            for(var j=0;j<polys.length;j++){
	            	eval("poly"+polys[j]).setFillColor("#DBA74D");
	            	eval("poly"+polys[j]).setFillOpacity(0.5);
	            	eval("poly"+polys[j]).setStrokeColor("#fff");
	            	eval("poly"+polys[j]).setStrokeWeight(1);
	            	map.addOverlay(eval("poly"+polys[j]));
	            }
	        }
    	}catch(err){
    		alert(err);
    	}
        
    }else{
        alert("IE9及以下浏览器不支持，请使用Chrome或Firefox浏览器");
    }

}

initMap();